module.exports = function(options) {
    var ajaxConfig = $.extend({
        type: config.type == 'test' ? 'GET' : 'POST',
        dataParser: true,
        showLoading: false,
        dataType: 'json',
        contentType: 'application/json',
        data: $.getRequest(),
        error: function(data) {
            if (!!data.resultMessage) {
                $.alert(data.resultMessage);
            } else if (data.readyState == 4 && data.status == 0) {
                $.alert("请求超时 !");
            }
            console.log(arguments);
        }
    }, options);

    if (ajaxConfig.showLoading) {
        var timer;
        ajaxConfig.beforeSend = (function(send) {
            return function() {
                timer = setTimeout(function() {
                    $.showPreloader();
                }, 100);
                if (!!send)
                    return send.apply(this, Array.prototype.slice.call(arguments));
            }
        })(ajaxConfig.beforeSend);
        ajaxConfig.complete = (function(complete) {
            return function() {
                clearTimeout(timer);
                $.hidePreloader();
                if (!!complete)
                    return complete.apply(this, Array.prototype.slice.call(arguments));
            }

        })(ajaxConfig.complete);
    }

    if (ajaxConfig.url.indexOf('http') < 0) ajaxConfig.url = config.path + ajaxConfig.url;
    ajaxConfig.data = JSON.stringify(ajaxConfig.data);
    ajaxConfig.success = (function(success) {
        return function(d) {
            if (d.resultCode != '0') {
                ajaxConfig.error(d);
                return;
            }
            if (!!d.attach && !!d.attach.data && ajaxConfig.dataParser) {
                with(d.attach) {
                    $.each(data, function(i) {
                        var obj = {};
                        for (var key in head) {
                            obj[head[key]] = this[key];
                        }
                        data[i] = obj;
                    });
                }
            }
            success.apply(this, Array.prototype.slice.call(arguments));
        }
    })(ajaxConfig.success);
    // console.log(ajaxConfig);
    return $.ajax(ajaxConfig);
};
